﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 库区
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class AreaController : BaseController
    {
        private readonly HttpClientHelper _httpClientFactory;
        private readonly IAreaService areaService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="areaService"></param>
        public AreaController(IWebHostEnvironment environment, IAreaService areaService, HttpClientHelper httpClientFactory) : base(environment)
        {
            this.areaService = areaService;
            _httpClientFactory = httpClientFactory;
        }
        /// <summary>
        /// 添加库区
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddArea(Area obj)
        {
            string message = "";
            if (string.IsNullOrEmpty(obj.Code) || obj.Code.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }        
            obj.CreateUserId = base.BaseGetNowUserId();
            obj.CreateUserName = base.BaseGetNowUserName();
            bool result = areaService.AddArea(obj, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！"+message, "500"));
        }
      
        /// <summary>
        /// 显示库区信息
        /// </summary>
        /// <param name="kname"></param>
        /// <param name="wid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAreanList(string? kname = "", long wid = 0, int pageIndex = 1, int pageSize = 20)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = areaService.GetAreanList(kname, wid, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 删除库区
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelArea(long uid)
        {
            if (uid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
         
            bool IsOK = areaService.DelArea(uid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 批量删除库区
        /// </summary>
        /// <param name="uidList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelAreaList(long[] uidList)
        {
            if (uidList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool daRes = true;
            ApiResult apiresult = new ApiResult();
            foreach (var uid in uidList)
            {
                bool IsOK = areaService.DelArea(uid);                     
                if (!IsOK)
                {
                    daRes = false;
                    break;
                }
            }

            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 修改库区信息
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateArea(Area u)
        {
            if (u.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(u.Code) || u.Code.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
   
            string message = "";
            u.ModifyUserId = base.BaseGetNowUserId();
            u.ModifyUserName = base.BaseGetNowUserName();
            var result = areaService.UpdateArea(u, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "修改失败！"+message, "500"));
        }
        /// <summary>
        /// 根据id获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAreaById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = areaService.GetAreaById(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));

        }

        /// <summary>
        /// 根据库房获取库区
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWhIdAreaList(long wid)
        {
            if (wid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }

            var list = areaService.GetWhIdAreaList(wid);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "获取成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "获取失败", 0));
        }

        /// <summary>
        /// 导出测试
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DowLoadExcel()
        {
            var list = areaService.GetAreanList("", 0, 1, 2, out int totalCount);
            return base.BaseDowLoadExcel(list);
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="excel"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadExcel(IFormFile excel)
        {
            if (excel == null || excel.Length == 0)
            {
                return Ok(BaseResult.Result("400", "没有选择文件或文件为空"));
            }
            var postFile = Request.Form.Files[0];
            string extName = Path.GetExtension(postFile.FileName);
            if (!new string[] { ".xls", ".xlsx" }.Contains(extName))
            {
                return Ok(BaseResult.Result("400", "必须是Excel文件"));
            }
            var stream = new MemoryStream();
            excel.CopyTo(stream);
            var rows = MiniExcel.Query<Area>(stream);

            List<Area> alist = new List<Area>();
            alist=rows.ToList();
            return Ok(alist);
        }

        [HttpPost]
        public async Task<IActionResult> CeShiHttp()
        {
            var response = await _httpClientFactory.GetAsync<ApiResult>("http://192.168.1.36:8004/api/Role/GetRoleList?state=3&pageIndex=1&pageSize=5",null,30);
        
            return Ok(response);
        }
    }
}
